package com.cdp.web.controller.system;

import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysCement;
import com.cdp.common.utils.poi.ExcelUtil;
import com.cdp.system.dto.CementDTO;
import com.cdp.system.dto.RawDTO;
import com.cdp.system.service.ISysCementService;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SandFineneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cdp.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 水泥信息
 */
@RestController
@RequestMapping("/system/cement")
public class SysCementController {

    @Autowired
    private ISysCementService sysCementService;

    /**
     * 查询集合
     *
     * @param rawDTO
     * @return
     */


    @PostMapping("/listRawData")
    public AjaxResult listRawData(@RequestBody RawDTO rawDTO) {
        Map<String, Object> data = sysCementService.listRawData(rawDTO);
        return AjaxResult.success(data);
    }


    /**
     * 下拉框展示
     *
     */
    @GetMapping("/listPullDown")
    public AjaxResult listPullDown() {
        Map<String, Object> listPullDown = sysCementService.listPullDown();
        return AjaxResult.success(listPullDown);
    }


    /**
     * 水泥模糊查询
     */

    @PostMapping("listMistedCement")
    public AjaxResult listMistedCement(@RequestBody CementDTO cementDTO) {
        try {

            return sysCementService.listMistedCement(cementDTO);
        } catch (Exception e) {
            log.error("查询水泥信息失败，参数为: {}", cementDTO, e);
            return AjaxResult.error("查询水泥信息失败: " + e.getMessage());
        }
    }


    /**
     * 水泥id模糊查询
     */

    @GetMapping("/queryCemenIdsById")
    public AjaxResult queryCemenIdsById(@RequestParam(required = false) Integer id) {
        List<IdsVO> idsVOS =sysCementService.queryCemenIdsById(id);

       // 将 idsVOS 中的 ids 列表转换为所需格式
        List<SandFineneVO> sandFinenessVOList = idsVOS.stream()
                .flatMap(idsVO -> idsVO.getIds().stream()) // 提取 ids 列表
                .map(idValue -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(String.valueOf(idValue)); // 将 Integer 转为 String
                    vo.setLabel(String.valueOf(idValue));
                    return vo;
                })
                .collect(Collectors.toList());

       // 返回所需格式数据
        return AjaxResult.success(sandFinenessVOList);

    }





}
